package pers.cz.util;

/**
 * @program: PostGirl-panent
 * @description: StringUtils
 * @author: Cheng Zhi
 * @create: 2022-04-05 13:05
 **/
public class StringUtils {

    /**
     * 将给定单词首字母转化为大写
     * @param word
     * @return
     */
    public static String returnFirstCaps(String word) {

        if (word.length() == 0) {
            return word;
        }

        char[] chars = word.toCharArray();
        char firstLetter = chars[0];
        // 先判断是否属于字母
        if (firstLetter >= 'a' && firstLetter <= 'z') {
            firstLetter = (char) (firstLetter - 32);
        }
        chars[0] = firstLetter;
        StringBuilder sb = new StringBuilder();
        for (char c : chars) {
            sb.append(c);
        }
        return sb.toString();
    }

    /**
     * 将给定单词首字母转化为小写
     * @param word
     * @return
     */
    public static String returnFirstLower(String word) {

        if (word.length() == 0) {
            return word;
        }

        char[] chars = word.toCharArray();
        char firstLetter = chars[0];
        // 先判断是否属于字母
        if (firstLetter >= 'A' && firstLetter <= 'Z') {
            firstLetter = (char) (firstLetter + 32);
        }
        chars[0] = firstLetter;
        StringBuilder sb = new StringBuilder();
        for (char c : chars) {
            sb.append(c);
        }
        return sb.toString();
    }

    /**
     * 将给定单词转化为驼峰命名法
     * @param word
     * @return
     */
    public static String getCamelCase(String word) {

        if (!word.contains("_")) {
            return returnFirstCaps(word);
        }

        String[] s = word.split("_");
        StringBuilder sb = new StringBuilder();
        for (String splitWord : s) {

            sb.append(returnFirstCaps(splitWord));
        }
        return sb.toString();
    }


    public static void main(String[] args) {
        System.out.println(getCamelCase("test_case_auto"));
    }
}
